note
    description: "Logger, appends to file specified in log_path. Change this in settings file."
    author: "togge, carolinux"
    date: "$Date$"
    revision: "$Revision$"

class
    LOGGER
create make

feature -- Access
    settings_path: attached STRING;
    log_path: attached STRING;
    writer: PLAIN_TEXT_FILE;


    -- appends the entry to the log file with a timestamp
    log (entry : separate STRING)
        local
            str_len : INTEGER_32;
            i : INTEGER;
            local_string : STRING_32;
            now : DATE_TIME;
        do    -- copy the string to a local one

            str_len := entry.count;
            create local_string.make_empty;

            from i := 0;
            until i = str_len
            loop
                i := i + 1;
                local_string.append_character (entry @ i);
            end

            create now.make_now; -- create timestamp and write to file
            writer.put_string ("[" + now.out + "]: " + local_string);
            writer.put_new_line;
            writer.flush

        end

    -- sets up the logger and writes a start message to the log.
    make
            -- `new'
        local
            init_file : PLAIN_TEXT_FILE;
            now : DATE_TIME;
        do
            -- look for log_path in the settings
            settings_path := "settings";
            create init_file.make (settings_path)

            if init_file.exists then

                init_file.open_read
                init_file.read_line;
                if attached {STRING_8} init_file.last_string as ls then
                    log_path := ls;
                else
                    log_path := "server.log";
                end
            else
                -- default value
                log_path := "server.log"
            end

            -- create the writer and log a start message
            create writer.make_open_append (log_path);
            create now.make_now;

            writer.put_string("[" + now.out + "]: Logger started.");
            writer.put_new_line;
            writer.flush
            print ("Logger started: " + now.out+"%N");
        end

end


